package com.njbw.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njbw.entity.PageResponse;
import com.njbw.user.convert.SysUserConverter;
import com.njbw.user.entity.po.SysUserPo;
import com.njbw.user.entity.req.SysUserReq;
import com.njbw.user.mapper.SysUserMapper;
import com.njbw.user.service.SysUserService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (SysUser)表服务实现类
 *
 * @author makejava
 * @since 2023-11-28 18:18:51
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserPo> implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    // 无法手动设置过期时间，可能导致整体的 key 都是我们设置的时间过期，就可能会产生缓存雪崩的现象
    // 采用这种方式就得配合 @CacheEvict 和 @CachePut 使用
    @Cacheable(cacheNames = "sysUser", key = "'querSysUserById'+#id") // # 可以拿到方法参数，key 就是 redisKey 不是很常用，不建议使用
    public SysUserPo queryById(Long id) {
        return this.sysUserMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysUserReq 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResponse<SysUserPo> queryByPage(SysUserReq sysUserReq) {
        // SysUserPo sysUserPo = new SysUserPo();
        // BeanUtils.copyProperties(sysUserReq, sysUserPo);
        SysUserPo sysUserPo = SysUserConverter.INSTANCE.convertReqToPo(sysUserReq);
        PageResponse<SysUserPo> pageResponse = new PageResponse<>();
        Long pageNo = sysUserReq.getPageNo(), pageSize = sysUserReq.getPageSize();
        pageResponse.setCurrent(pageNo);
        pageResponse.setPageSize(pageSize);
        // 计算页码起始位置
        Long pageStart = (pageNo - 1) * pageSize;
        long total = this.sysUserMapper.count(sysUserPo);
        List<SysUserPo> sysUserPoList = this.sysUserMapper.queryByPage(sysUserPo, pageStart, pageSize);
        pageResponse.setRecords(sysUserPoList);
        pageResponse.setTotal(total);
        return pageResponse;
    }

    @Override
    public List<SysUserPo> queryAll(SysUserPo sysUserPo) {
        return this.sysUserMapper.queryAll(sysUserPo);
    }

    /**
     * 新增数据
     *
     * @param sysUserPo 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(SysUserPo sysUserPo) {
        return this.sysUserMapper.insert(sysUserPo) > 0;
    }

    /**
     * 修改数据
     *
     * @param sysUserPo 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(SysUserPo sysUserPo) {
        return this.sysUserMapper.update(sysUserPo) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sysUserMapper.deleteById(id) > 0;
    }

    @Override
    public Long queryCount(Map<String, Object> conditions) {
        SysUserPo sysUserPo = new SysUserPo();
        return sysUserMapper.count(sysUserPo);
    }

    @Override
    public List<SysUserPo> queryByExport(Map<String, Object> conditions) {
        SysUserPo sysUserPo = new SysUserPo();
        sysUserPo.setId(Long.valueOf(conditions.get("id").toString()));
        return sysUserMapper.queryAll(sysUserPo);
    }
}
